<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="子程序一般都是完成某种特定功能的程序段。当一个程序调用一个子程序时，通常都向子程序传递若干个数据让它来处理；当子程序处理完后，一般也向调用它的程序传递处理结果，我们称这种在调用程序和子程序之间的信息传递为参数传递。 用程序向子程序传递的参数称为子程序的入口参数，子程序向调用它的程序传递的参数称为子程序的出口参数。子程序的入口参数和出口参数都是任意项，对某个具体的子程序来说，要根据具体情况来确定其入">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="7.3 子程序的参数传递">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2015/04/01/asm30/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="子程序一般都是完成某种特定功能的程序段。当一个程序调用一个子程序时，通常都向子程序传递若干个数据让它来处理；当子程序处理完后，一般也向调用它的程序传递处理结果，我们称这种在调用程序和子程序之间的信息传递为参数传递。 用程序向子程序传递的参数称为子程序的入口参数，子程序向调用它的程序传递的参数称为子程序的出口参数。子程序的入口参数和出口参数都是任意项，对某个具体的子程序来说，要根据具体情况来确定其入">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://www.iteedu.com/plang/asm/asmhbyychxshj/images/clip_image002_0038.gif">
<meta property="og:image" content="http://www.iteedu.com/plang/asm/asmhbyychxshj/images/clip_image003_0019.gif">
<meta property="og:image" content="hhttp://www.iteedu.com/plang/asm/asmhbyychxshj/images/clip_image004_0015.gif">
<meta property="og:image" content="http://www.iteedu.com/plang/asm/asmhbyychxshj/images/clip_image005_0007.gif">
<meta property="og:updated_time" content="2019-01-21T13:59:48.421Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="7.3 子程序的参数传递">
<meta name="twitter:description" content="子程序一般都是完成某种特定功能的程序段。当一个程序调用一个子程序时，通常都向子程序传递若干个数据让它来处理；当子程序处理完后，一般也向调用它的程序传递处理结果，我们称这种在调用程序和子程序之间的信息传递为参数传递。 用程序向子程序传递的参数称为子程序的入口参数，子程序向调用它的程序传递的参数称为子程序的出口参数。子程序的入口参数和出口参数都是任意项，对某个具体的子程序来说，要根据具体情况来确定其入">
<meta name="twitter:image" content="http://www.iteedu.com/plang/asm/asmhbyychxshj/images/clip_image002_0038.gif">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2015/04/01/asm30/">





  <title>7.3 子程序的参数传递 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2015/04/01/asm30/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">7.3 子程序的参数传递</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2015-04-01T20:35:55+08:00">
                2015-04-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/汇编教程/" itemprop="url" rel="index">
                    <span itemprop="name">汇编教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>子程序一般都是完成某种特定功能的程序段。当一个程序调用一个子程序时，通常都向子程序传递若干个数据让它来处理；当子程序处理完后，一般也向调用它的程序传递处理结果，我们称这种在调用程序和子程序之间的信息传递为参数传递。</p>
<p>用程序向子程序传递的参数称为子程序的入口参数，子程序向调用它的程序传递的参数称为子程序的出口参数。子程序的入口参数和出口参数都是任意项，对某个具体的子程序来说，要根据具体情况来确定其入口和出口参数，也可以二者都没有。</p>
<p>程序和被调用子程序之间的参数传递方法是程序员自己或和别人事先约定好的信息传递方法。这种信息传递方法可以是多种多样的，在本节，我们只介绍常用的、行之有效的参数传递方法有：寄存器传递参数、约定存储单元传递参数和堆栈传递参数等。如果对其它的参数传递方法感兴趣的话，可参考其它《汇编语言程序设计》书籍。</p>
<hr>
<h2 id="7-3-1-寄存器传递参数"><a href="#7-3-1-寄存器传递参数" class="headerlink" title="7.3.1 寄存器传递参数"></a>7.3.1 寄存器传递参数</h2><p>一方面，由于CPU中的寄存器在任何程序中都是“可见”的，一个程序对某寄存器赋值后，在另一个程序中就能直接使用，所以，用寄存器来传递参数最直接、简便，也是最常用的参数传递方式。但另一方面，CPU中寄存器的个数和容量都是非常有限，所以，该方法适用于传递较少的参数信息。</p>
<p><strong>例7.1是用寄存器传递参数的例子，子程序处理的数据被保存在寄存器AL中。假设有下列的程序段：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">…</span><br><span class="line"></span><br><span class="line">MOV AL, &apos;b&apos;</span><br><span class="line"></span><br><span class="line">CALL UPPER ;子返回时，(AL)=&apos;B&apos;</span><br><span class="line"></span><br><span class="line">…</span><br><span class="line"></span><br><span class="line">MOV AL, &apos;2&apos;</span><br><span class="line"></span><br><span class="line">CALL UPPER ;子返回时，AL的值不变，因为&apos;2&apos;不是字母 </span><br><span class="line"></span><br><span class="line">…</span><br></pre></td></tr></table></figure>
<p><strong>例7.3 按五位十进制的形式显示寄存器BX中的内容，如果BX的值小于0，则应在显示数值之前显示负号’-‘。</strong></p>
<p>例如：(BX)=123，显示：00123；(BX)=-234，显示：-00234；</p>
<p>解：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line">;子程序功能：把寄存器BX的内容按十进制有符号数显示出来 </span><br><span class="line"></span><br><span class="line">;入口参数：BX</span><br><span class="line"></span><br><span class="line">;出口参数：无，只有显示信息 </span><br><span class="line"></span><br><span class="line">;算法描述： </span><br><span class="line"></span><br><span class="line">1、定义6个字节的存储单元 </span><br><span class="line"></span><br><span class="line">2、先判断BX是否小于零，如果是，则先显示负号&apos;-&apos;，再取BX的绝对值； </span><br><span class="line"></span><br><span class="line">3、采用除10，得余数的方法，从低位向高位求出每位十进制位； </span><br><span class="line"></span><br><span class="line">4、输出数据的字符串。 </span><br><span class="line"></span><br><span class="line">SubData SEGMENT</span><br><span class="line"></span><br><span class="line">DB 5 DUP(&apos;0&apos;), 0ah, 0dh, &apos;$&apos;　;0ah、0dh：换行、回车 </span><br><span class="line"></span><br><span class="line">SubData ENDS</span><br><span class="line"></span><br><span class="line">DISPBX PROC</span><br><span class="line"></span><br><span class="line">ASSUME DS:SubData</span><br><span class="line"></span><br><span class="line">PUSH DS</span><br><span class="line"></span><br><span class="line">PUSH DX</span><br><span class="line"></span><br><span class="line">PUSH CX</span><br><span class="line"></span><br><span class="line">PUSH AX</span><br><span class="line"></span><br><span class="line">MOV AX, SubData ;取子程序所用的数据区段地址 </span><br><span class="line"></span><br><span class="line">MOV DS, AX</span><br><span class="line"></span><br><span class="line">CMP BX, 0</span><br><span class="line"></span><br><span class="line">JGE next</span><br><span class="line"></span><br><span class="line">MOV DL, &apos;-&apos;</span><br><span class="line"></span><br><span class="line">MOV AH, 2</span><br><span class="line"></span><br><span class="line">INT 21H ;显示负号&apos;-&apos;</span><br><span class="line"></span><br><span class="line">NEG BX ;求-BX，使其值为正数 </span><br><span class="line"></span><br><span class="line">next: MOV SI, 4</span><br><span class="line"></span><br><span class="line">MOV AX, BX</span><br><span class="line"></span><br><span class="line">MOV CX, 10D</span><br><span class="line"></span><br><span class="line">again: XOR DX, DX</span><br><span class="line"></span><br><span class="line">IDIV CX ;DX存放余数，AX存放商 </span><br><span class="line"></span><br><span class="line">ADD DL, &apos;0&apos;</span><br><span class="line"></span><br><span class="line">MOV [SI], DL</span><br><span class="line"></span><br><span class="line">DEC SI</span><br><span class="line"></span><br><span class="line">JGE again</span><br><span class="line"></span><br><span class="line">XOR DX, DX</span><br><span class="line"></span><br><span class="line">MOV AH, 9</span><br><span class="line"></span><br><span class="line">INT 21H ;调用中断21的功能9，显示DS:DX指向的字符串 </span><br><span class="line"></span><br><span class="line">POP AX</span><br><span class="line"></span><br><span class="line">POP CX</span><br><span class="line"></span><br><span class="line">POP DX</span><br><span class="line"></span><br><span class="line">POP DS</span><br><span class="line"></span><br><span class="line">RET</span><br><span class="line"></span><br><span class="line">DISPBX ENDP</span><br></pre></td></tr></table></figure>
<h2 id="7-3-2-约定存储单元传递参数"><a href="#7-3-2-约定存储单元传递参数" class="headerlink" title="7.3.2 约定存储单元传递参数"></a>7.3.2 约定存储单元传递参数</h2><p>在调用子程序时，当需要向子程序传递大量数据时，因受到寄存器容量的限制，就不能采用寄存器传递参数的方式，而要改用约定存储单元的传送方式。这种参数传递方式有点象情报人员和联络人员之间的传递信息方式，一个向指定地点放情报，另一个从指定地点取情报。</p>
<p>例7.2是采用约定存储单元传递参数的例子，所处理的数据不是直接传给子程序，而是把存储它们的地址告诉子程序。</p>
<p><strong>例7.4：编写一个子程序分类统计出一个字符串中数字字符、字母和其它字符的个数。该字符串的首地址用DS:DX来指定(以0为字符串结束)，各类字符个数分别存放BX、CX和DI中。</strong></p>
<p>解：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line">;子程序功能：分类统计出字符串中数字字符、字母和其它字符的个数 </span><br><span class="line"></span><br><span class="line">;入口参数：DS:DX指向被统计的字符串 </span><br><span class="line"></span><br><span class="line">;出口参数：BX、CX和DI分别保存数字字符、字母和其它字符的个数 </span><br><span class="line"></span><br><span class="line">;算法描述： </span><br><span class="line"></span><br><span class="line">1、当字符在&apos;0&apos;~&apos;9&apos;范围时，数字字符个数BX加1； </span><br><span class="line"></span><br><span class="line">2、为了判断简单，先把字字母变成大写字母； </span><br><span class="line"></span><br><span class="line">3、当字符在&apos;A&apos;~&apos;Z&apos;范围时，字母个数CX加1； </span><br><span class="line"></span><br><span class="line">4、否则，其它字符个数DI加1。 </span><br><span class="line"></span><br><span class="line">COUNT PROC</span><br><span class="line"></span><br><span class="line">PUSH AX</span><br><span class="line"></span><br><span class="line">PUSH SI</span><br><span class="line"></span><br><span class="line">XOR BX, BX</span><br><span class="line"></span><br><span class="line">XOR CX, CX</span><br><span class="line"></span><br><span class="line">XOR DI, DI ;上三条指令使各类字符计数清零 </span><br><span class="line"></span><br><span class="line">MOV SI, DX </span><br><span class="line"></span><br><span class="line">again: MOV AL, [SI]</span><br><span class="line"></span><br><span class="line">INC SI</span><br><span class="line"></span><br><span class="line">CMP AL, 0</span><br><span class="line"></span><br><span class="line">JE over</span><br><span class="line"></span><br><span class="line">CMP AL, &apos;0&apos;</span><br><span class="line"></span><br><span class="line">JL other</span><br><span class="line"></span><br><span class="line">CMP AL, &apos;9&apos;</span><br><span class="line"></span><br><span class="line">JG next</span><br><span class="line"></span><br><span class="line">INC BX ;数字字符个数加1</span><br><span class="line"></span><br><span class="line">JMP again</span><br><span class="line"></span><br><span class="line">next: CALL UPPER ;调用子程序把AL中的字母变成大写字母 </span><br><span class="line"></span><br><span class="line">CMP AL, &apos;A&apos;</span><br><span class="line"></span><br><span class="line">JL other</span><br><span class="line"></span><br><span class="line">CMP AL, &apos;Z&apos;</span><br><span class="line"></span><br><span class="line">JG other</span><br><span class="line"></span><br><span class="line">INC CX ;字母个数加1</span><br><span class="line"></span><br><span class="line">JMP again</span><br><span class="line"></span><br><span class="line">other: INC DI ;其它字符个数加1</span><br><span class="line"></span><br><span class="line">JMP again</span><br><span class="line"></span><br><span class="line">over: POP SI</span><br><span class="line"></span><br><span class="line">POP AX</span><br><span class="line"></span><br><span class="line">RET</span><br><span class="line"></span><br><span class="line">COUNT ENDP</span><br></pre></td></tr></table></figure>
<p><strong>例7.5 显示出任意字符串中数字字符、字母和其它字符的个数。</strong></p>
<p>解：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">.MODEL SMALL</span><br><span class="line"></span><br><span class="line">.DATA</span><br><span class="line"></span><br><span class="line">MSG DB　&apos;KSDJ L0984/[]3oiu OIU OIU (*&amp;(5341&apos;, 0</span><br><span class="line"></span><br><span class="line">.CODE</span><br><span class="line"></span><br><span class="line">.STARTUP</span><br><span class="line"></span><br><span class="line">LEA DX, MSG ;DS:DX指向待统计的字符串 </span><br><span class="line"></span><br><span class="line">CALL COUNT ;调用子程序统计出各类字符的个数 </span><br><span class="line"></span><br><span class="line">CALL DISPBX ;调用子程序显示数字字符的个数 </span><br><span class="line"></span><br><span class="line">MOV BX, CX</span><br><span class="line"></span><br><span class="line">CALL DISPBX?;调用子程序显示字母的个数 </span><br><span class="line"></span><br><span class="line">MOV BX, DI</span><br><span class="line"></span><br><span class="line">CALL DISPBX ;调用子程序显示其它字符的个数 </span><br><span class="line"></span><br><span class="line">.EXIT　0</span><br><span class="line"></span><br><span class="line">END</span><br></pre></td></tr></table></figure>
<h2 id="7-3-3-堆栈传递参数"><a href="#7-3-3-堆栈传递参数" class="headerlink" title="7.3.3 堆栈传递参数"></a>7.3.3 堆栈传递参数</h2><p>堆栈是一个特殊的数据结构，它通常是用来保存程序的返回地址。当用它来传递参数时，势必会造成数据和返回地址混合在一起的局面，用起来要特别仔细。</p>
<p>具体做法如下：</p>
<ol>
<li>当用堆栈传递入口参数时，要在调用子程序前把有关参数依次压栈，子程序从堆栈中取到入口参数；</li>
<li>当用堆栈传递出口参数时，要在子程序返回前，把有关参数依次压栈(这里还需要做点额外操作，要保证返回地址一定在栈顶)，调用程序就可以从堆栈中取到出口参数。</li>
</ol>
<p>在通常情况下，我们用堆栈传入口参数，用寄存器传出口参数。</p>
<p><strong>1、用堆栈传递入口参数的调用方法</strong>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">…</span><br><span class="line"></span><br><span class="line">PUSH Para1</span><br><span class="line"></span><br><span class="line">…</span><br><span class="line"></span><br><span class="line">PUSH Paran ;把n个字的参数压栈 </span><br><span class="line"></span><br><span class="line">CALL? SUBPRO ;调用子程序SUBPRO</span><br><span class="line"></span><br><span class="line">…</span><br></pre></td></tr></table></figure>
<p><strong>2、在子程序中取入口参数的方法：</strong></p>
<ul>
<li>段内调用子程序</li>
</ul>
<p>由于是段内调用，所以，CALL指令只把返回地址的偏移量(即IP的内容)压栈，如图7.6(a)所示。在进入子程序后，为了能读取传递过来的参数，需要用BP来访问堆栈，所以要先保护BP原来的值，再把当前SP的值传送给BP。</p>
<p>于是，当前BP所指向的堆栈单元与最后一个参数Paran之间隔着BP的原值和返回地址的偏移量，也就是说：二者之间相差4个字节。具体情况如图7.6(b)所示。</p>
<p><img src="http://www.iteedu.com/plang/asm/asmhbyychxshj/images/clip_image002_0038.gif" alt></p>
<center>(a)、进入子程序时堆栈情况</center>

<p><img src="http://www.iteedu.com/plang/asm/asmhbyychxshj/images/clip_image003_0019.gif" alt></p>
<center>(b)、子程序寄存器保护后的堆栈情况</center>

<center><strong>图7.6 在段内调用情况下子程序所能访问的堆栈情况</strong></center>

<p>在子程序中读取用堆栈传递参数的一般方法如下程序片段所示。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">SUBPRO PROC?NEAR</span><br><span class="line"></span><br><span class="line">PUSH BP ;保护寄存器BP</span><br><span class="line"></span><br><span class="line">MOV BP, SP ;用寄存器BP来访问堆栈，读取参数 </span><br><span class="line"></span><br><span class="line">… ;保护其它寄存器的指令 </span><br><span class="line"></span><br><span class="line">MOV Paran, [BP+4] ;保护其它寄存器的指令 </span><br><span class="line"></span><br><span class="line">…</span><br><span class="line"></span><br><span class="line">MOV Para1, [BP+4+2*(n-1)]</span><br><span class="line"></span><br><span class="line">…</span><br><span class="line"></span><br><span class="line">SUBPRO ENDP</span><br></pre></td></tr></table></figure>
<ul>
<li>段间调用子程序</li>
</ul>
<p>在段间调用子程序时，CALL指令会把返回地址的偏移量和段寄存器CS的内容都压栈，如图7.7(a)所示。在进入子程序后，与前面“段内调用子程序”一样，也需要用BP来读取传递过来的参数，所以，也要先保护BP原来的值，再把当前SP的值传送给BP。</p>
<p>这时，当前BP所指向的堆栈单元与最后一个参数Paran之间隔着BP的原值、返回地址的偏移量和段地址，所以，二者之间相差6个字节。具体情况如图7.7(b)所示。</p>
<p><img src="hhttp://www.iteedu.com/plang/asm/asmhbyychxshj/images/clip_image004_0015.gif" alt></p>
<center>(a)、进入子程序时堆栈情况</center>

<p><img src="http://www.iteedu.com/plang/asm/asmhbyychxshj/images/clip_image005_0007.gif" alt></p>
<center>(b)、子程序寄存器保护后的堆栈情况</center>

<center><strong>图7.7 在段间调用情况下子程序所能访问的堆栈情况</strong></center>

<p>在段间调用时，除了多一个返回段地址外，其它的内容与“段内调用”的情况完全一致，所以，在读取第i个参数时，只要用[BP+6+4*(n-i)]代替[BP+4+2(n-i)]即可(假设每个参数都是字类型)。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2015/04/01/asm29/" rel="next" title="7.2 子程序的调用和返回指令">
                <i class="fa fa-chevron-left"></i> 7.2 子程序的调用和返回指令
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2015/04/01/asm31/" rel="prev" title="7.4 寄存器的保护与恢复">
                7.4 寄存器的保护与恢复 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#7-3-1-寄存器传递参数"><span class="nav-number">1.</span> <span class="nav-text">7.3.1 寄存器传递参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-3-2-约定存储单元传递参数"><span class="nav-number">2.</span> <span class="nav-text">7.3.2 约定存储单元传递参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-3-3-堆栈传递参数"><span class="nav-number">3.</span> <span class="nav-text">7.3.3 堆栈传递参数</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
